<!-- 我的纸条页面 -->
<template>
	<view id="body">
		<view class="nav">
			<u-subsection :list="list" mode="button" :current="current" @change="change"></u-subsection>
		</view>
		<!-- 纸条内容 -->
		<view class="note-content">
			<view class="note-cart" v-for="(item,index) in noteList" :key="index" v-if="noteList.length > 0 & noteList != ''">
				<view class="time">
					<text>放入时间:</text>
					<text style="margin-left: 8rpx;">{{item.createTime}}</text>
				</view>
				<!-- 纸条标题 -->
				<view class="note-title">
					<text>纸条标题:</text>
					<text style="line-height: 40rpx;">{{item.title}}</text>
				</view>
				<!-- 按钮 -->
				<view class="note-button">
					<view class="info" @click="noteinfo(index)">
						<text style="line-height: 40rpx;" >纸条详情</text>
					</view>
					<!-- 有缘人 -->
					<view class="issueUser" @click="youyuanren(index)" v-if="current != 0">
						<text style="line-height: 40rpx;">有缘人</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 抽取的纸条模态框 -->
		<view>
			<u-modal :show="ztshow" @confirm="ztclose" >
				<view class="ztct">
					<view class="zttitle">
						<text>{{note.title}}</text>
					</view>
					<view class="ztimg">
						<image :src="note.issueUserimg" class="ztimg-img"></image>
					</view>
					<view class="ztusername">
						<text>{{note.issueUsername}}</text>
					</view>
					<view class="ztcreateTime">
						<text>{{note.createTime}}</text>
					</view> 
					<view class="ztcontent">
						<text>{{note.content}}</text>
					</view>
				</view>
			</u-modal>
		</view>
		<!-- 加载动画 -->
		<view class="loading" >
			<u-loadmore :status="status" :line="line" :nomoreText="nomoreText" /> 
		</view>
	</view>
</template>

<script>
	import {http} from "../static/service/requestAddress/rtas.js"
	export default {
		data() {
			return {
				list:['我防入的纸条','我抽取的纸条'],
				current:0,
				status:"loading",
				nomoreText:"——没有更多了——",
				noteList:[],
				note:{},//纸条详情
				ztshow:false,
			}
		},
		created(){
			uni.showLoading({
				title:"正在获取放入纸条",
				mask: true
			})
			var user = uni.getStorageSync('user')
			//验证用户是否登录
			if(! user){
				uni.$u.toast("您还未登录，登录后才能获取您放入的纸条")
				uni.hideLoading()
				return
			}
			uni.request({
				url:http+"note/getnote/0/"+JSON.parse(user).id,
				method:'GET', 
				complete:(res)=>{
					if(res.data.code == 20021){
						this.noteList = res.data.date
						uni.hideLoading()
						uni.$u.toast("获取放入纸条成功")
						this.status = "nomore"
						console.log("获取放入纸条成功:"+JSON.stringify(res.data.date))
					}else{
						uni.hideLoading()
						uni.$u.toast("获取放入纸条失败")
						this.status = "nomore"
					}
				}
			})
		},
		methods:{
			change(index){
				this.current = index
				if(index == 0){
					uni.showLoading({
						title:"正在获取放入纸条",
						mask: true
					})
					var user = uni.getStorageSync('user')
					//验证用户是否登录
					if(! user){
						uni.$u.toast("您还未登录，登录后才能获取您放入的纸条")
						uni.hideLoading()
						return
					}
					this.noteList = []
					uni.request({
						url:http+"note/getnote/0/"+JSON.parse(user).id,
						method:'GET', 
						complete:(res)=>{
							if(res.data.code == 20021){
								this.noteList = res.data.date
								uni.hideLoading()
								uni.$u.toast("获取放入纸条成功")
								this.status = "nomore"
								console.log("获取放入纸条成功:"+JSON.stringify(res.data.date))
							}else{
								uni.hideLoading()
								uni.$u.toast("获取放入纸条失败")
								this.status = "nomore"
							}
						}
					})
				}else{//获取用户抽取的纸条
					uni.showLoading({
						title:"正在获取抽取的纸条",
						mask: true
					})
					var user = uni.getStorageSync('user')
					//验证用户是否登录
					if(! user){
						uni.$u.toast("您还未登录，登录后才能获取您抽取的纸条")
						uni.hideLoading()
						return
					}
					this.noteList = []
					uni.request({
						url:http+"note/gettakeoutnote/0/"+JSON.parse(user).id,
						method:'GET', 
						complete:(res)=>{
							if(res.data.code == 20021){
								this.noteList = res.data.date
								uni.hideLoading()
								uni.$u.toast("获取抽取纸条成功")
								this.status = "nomore"
								console.log("获取抽取纸条成功:"+JSON.stringify(res.data.date))
							}else{
								uni.hideLoading()
								uni.$u.toast("获取抽取纸条失败")
								this.status = "nomore"
							}
						}
					})
				}
			},
			//纸条详情
			noteinfo(index){
				console.log("点击了纸条详情"+JSON.stringify(this.noteList[index]))
				this.note = this.noteList[index]
				this.ztshow = true
			},
			//有缘人
			youyuanren(index){
				//添加用户到消息列表
				uni.navigateTo({
					url:"/pages/see/chatpage"+"?id="+this.noteList[index].issueUserid+"&name="+this.noteList[index].issueUsername+"&img="+this.noteList[index].issueUserimg
				})
			},
			ztclose(){
				this.ztshow = false
			}
		}
	}
</script> 

<style>
	@import url("../static/css/componentsCss/makefriends/mynote.css");
</style>